package com.learn.rocketmq.simpletest;

import com.learn.rocketmq.simple.Producer;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.UnsupportedEncodingException;

/**
 * @date 2021/8/16
 */
public class ProducerTest {

    public static void main(String[] args) throws UnsupportedEncodingException, MQBrokerException, RemotingException, InterruptedException, MQClientException {

        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:producer.xml");

        Producer producer = context.getBean(Producer.class);

        for (int i = 0; i < 10; i++) {
            // Message Body
            byte[] body = ("Hello RocketMQ Spring " + i).getBytes(RemotingHelper.DEFAULT_CHARSET);
            Message msg = new Message("LearnTopic", "DonTag", body);
            msg.setKeys("key-" + i);
            SendResult sendResult = producer.getProducer().send(msg);

            System.out.println(sendResult);
        }

    }
}
